package tum0r.webengine.models.core

import tum0r.webengine.config.ApplicationConfig
import java.io.File

/**
 * 说明: 静态资源数据类
 *
 * 创建者: tum0r
 *
 * 时间: 2021/8/21 20:29
 *
 * @param[resourceDirectory] 静态资源所在文件夹
 * @param[resourceMapping] 静态资源映射路径
 * @param[indexFile] 静态资源首页文件
 */
data class StaticResource(val resourceDirectory: File, var resourceMapping: String, val indexFile: File) {

    init {
        resourceMapping = ApplicationConfig.regexRequestURL.replace(resourceMapping, "")
    }

    /**
     * @param[dirPath] 静态资源所在文件夹路径
     * @param[mapping] 静态资源映射路径
     * @param[indexPath] 静态资源首页文件路径
     */
    constructor(dirPath: String, mapping: String, indexPath: String) : this(File(dirPath), "/$mapping", File(indexPath))

    /**
     * 比较映射路径是否相同或
     *
     * @param[mapping] 请求路径
     */
    fun compare(mapping: String): Boolean = if (mapping.isBlank()) resourceMapping == ("/" + ApplicationConfig.SERVER_NAME) else resourceMapping == mapping
}